<?php
namespace Admin\Controller;

use Think\Controller;

class CustInfoController extends Controller
{

    public function custInfo()
    {   sess();
        $cust = M('Cust');
        $map = _search($dbname = "Cust");
        $map['uid']=session('uid');
        $count = $cust->where($map)->count();
        $Page = getpage($count, 3); // 实例化分页类 传入总记录数
        $show = $Page->show(); // 分页显示输出
        $list = $cust->where($map)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        updateTime();
        $this->display();
    }

    public function outxls()
    {sess();
        $model = M('Cust');
        $list = $model->field('id,fenlei,title,xcrq,xingming,phone,qq,type,uname,addtime,updatetime')->select();
        $headArr = array(
            'ID',
            '进展',
            '公司名称',
            '下次联系',
            '联系人',
            '手机号码',
            'QQ',
            '客户来源',
            '添加人',
            '添加时间',
            '更新时间'
        );
        $filename = '客户信息';
        xlsout($filename, $headArr, $list);
        updateTime();
    }

    public function add()
    {sess();
        if (IS_POST) {
            if (add("Cust")) {
                $this->success('新增成功', U('CustInfo/custInfo'));
            } else {
                $this->error('新增失败');
            }
        }
        updateTime();
        $this->display();
    }

    public function edit()
    {sess();
        $model = M('Cust');
        $Rs = $model->getById(I('get.id'));
        $this->assign('Rs', $Rs);
        if (IS_POST) {
            if (edit("Cust")) {
                $this->success('修改成功', U('CustInfo/custInfo'));
            } else {
                $this->error('修改失败');
            }
        }
        updateTime();
        $this->display();
    }

    public function del()
    {sess();
        $model = M('Custp');
        $modeld = M('Cust');
        $map['xingming'] = $modeld->getFieldById(I('get.id'), 'xingming');
        $data['zt'] = 'y';
        $model->startTrans();
        // 进行相关的业务逻辑操作
        $del = $modeld->delete(I('get.id'));
        $save = $model->where($map)->save($data);
        if ($del && $save) {
            $model->commit();
            redirect(U('CustInfo/custInfo'));
        } else {
            $model->rollback();
            $this->error('删除失败');
        }
        // if (del("Cust")) {
        
        // $this->redirect('CustInfo/custInfo');
        // } else {
        // $this->error('删除失败');
        // }
        updateTime();
    }

    public function progress()
    {sess();
        $model = M('Custgd');
        $Rs = $model->getByJcid(I('get.cid'));
        $this->assign('Rs', $Rs);
        if (IS_POST) {
            if (edit("Custgd")) {
                $this->success('修改成功', U('CustInfo/custInfo'));
            } else {
                $this->error('修改失败');
            }
        }
        updateTime();
        $this->display();
    }
    
    public function _fenxi($dbname,$fd,$ft,$type) {
        import("Org.Util.Chart");
        $chart = new \Chart();
        $model = D($dbname);
        $this->fd=$fd;
        $map = _search($dbname);
        $map['uid']=session('uid');
        $list = $model->where($map)->distinct($this->fd)->field($this->fd)->select();
        foreach ($list as $key =>$vo){
            $info=$info.",".$vo[$this->fd];
            $co = $model->where(array($this->fd=>$vo[$this->fd],'uid'=>session('uid')))->count('id');
            $count=$count.",".$co;
        }
        $title = $ft;
        $data = explode(",", substr ($count, 1));
        $size = 140;
        $width = 650;
        $height = 350;
        $legend = explode(",", substr ($info, 1));
        ob_end_clean();
        if ($type == 1) {
            $chart->create3dpie($title,$data,$size,$height,$width,$legend);
        }
        if ($type == 2) {
            $chart->createcolumnar($title,$data,$size,$height,$width,$legend);
        }
        if ($type == 3) {
            $chart->createmonthline($title,$data,$size,$height,$width,$legend);
        }
        if ($type == 4) {
            $chart->createring($title,$data,$size,$height,$width,$legend);
        }
        $chart->createhorizoncolumnar($title, $subtitle, $data, $size, $height, $width, $legend);
    }
    
    public function type(){
        $this->_fenxi('Cust','type','客户来源',2);
    }  
         
    public function fenlei(){
       $this->_fenxi('Cust','fenlei','跟踪进度',4);
    }
    
    public function custlfenxi(){
        sess();
        $this->display();
    }
    public function gdjdfenxi(){
        sess();
        $this->display();
    }
    
    public function niandufenxi(){
        sess();
        $this->display();
    }
    public function niandu(){
        import("Org.Util.Chart");
        $chart = new \Chart();
        for($i=1;$i<=12;$i++){
            $info[($i-1)]=$i;
            if($i<10){
                $co = M('Cust')->where(array('addm'=>date("Y",time())."-0".$i,'uid'=>session('uid')))->count('id');
            }else{
                $co = M('Cust')->where(array('addm'=>date("Y",time())."-".$i,'uid'=>session('uid')))->count('id');
            }
            $count=$count.",".$co;
        }
        $title ='年度客户增长趋势';
        $data = explode(",", substr ($count, 1));
        $size = 140;
        $width = 650;
        $height = 350;
        ob_end_clean();
        $chart->createcolumnar($title,$data,$size,$height,$width,$info);
        $this->display();
    }
     
}